package net.zdreamer.geracomium.ui.home;

import java.util.ArrayList;
import java.util.List;

import net.zdreamer.geracomium.adapter.FragmentTabAdapter;
import net.zdreamer.geracomium.ui.AssessContentFragment;

import com.example.geracomium.R;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DoAssessActivity extends FragmentActivity implements
        OnClickListener {
    private ImageView back, next;
    private LinearLayout layout;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    public static int count = 0;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    public static int selectedId = -1;
    public static List<Integer> choices = new ArrayList<Integer>();
    private Intent intent;
    public List<String> questions = new ArrayList<String>();
    private JSONArray array;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do_assess);
        which();
        System.out.println(array.length());
        Fragment[] fragmentArray = new AssessContentFragment[array.length()];
        for (int i = 0; i < fragmentArray.length; i++) {
            fragmentArray[i] = new AssessContentFragment();
            fragments.add(fragmentArray[i]);
        }
        initViews();

    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void which() {
        intent = getIntent();
        intent.getIntExtra("which", 0);
        String table = intent.getStringExtra("table");
        // System.out.println(table);
        try {
            array = new JSONArray(table);
            for (int i = 0; i < array.length(); i++) {
                questions.add(array.getString(i));
                // System.out.println(array.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    void initViews() {
        count = 0;
        ((TextView) findViewById(R.id.title_content)).setText("在线评估");
        next = (ImageView) findViewById(R.id.btn_next_do_assess);
        next.setOnClickListener(this);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment_container, fragments.get(count));
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        if (v.equals(back)) {
            finish();
        } else {
            if (count == array.length() - 1) {
                int total=0;
                for (int i = 0; i < array.length(); i++) {
                     // System.out.println("结果--------->>" + choices.get(i));
                    total=total+choices.get(i);
                   // System.out.println("结果" + total);
                }
                Intent intent = new Intent(this, ResultOfAssessActivity.class);
                intent.putIntegerArrayListExtra("result", (ArrayList<Integer>) choices);
                startActivity(intent);
                this.finish();
            } else {
                if (selectedId == -1) {
                    Toast.makeText(this, "请选择后继续", Toast.LENGTH_SHORT).show();
                } else {
                    count++;
                    transaction = fragmentManager.beginTransaction();
                    transaction.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
                    transaction.replace(R.id.fragment_container,
                            fragments.get(count));
                    transaction.commit();
                    selectedId = -1;
                    if (count == array.length()-1) {
                        next.setImageResource(R.drawable.seletor_finish);
                    }
                }

            }
        }
    }
}